package com.kyrie.utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
/***
 * 描述:
 *
 * @author wuxiang
 * @date 2020-04-09 23:54
 */
public class RequestUtil {

    /**
     * 从request中获得参数Map,并返回可读的Map
     */
    public static Map getParameterMap(HttpServletRequest request){
        //参数Map
        Map properties = request.getParameterMap();
        //返回值Map
        Map returnMap = new HashMap();
        Iterator<String> it = properties.keySet().iterator();
        String name = "";
        String value = "";
        while(it.hasNext()){
            String key = it.next();
            Object valueObj = properties.get(key);
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[]) valueObj;
                for(int i = 0; i <values.length;i++){
                    value = values[i]+",";
                }
                value = value.substring(0,value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(key, value);
        }
        return returnMap;
    }

    public static Map<String,Object> trimParameter(Map<String,Object> param){
        //返回值Map
        Map<String,Object> returnMap = new HashMap<String,Object>();
        Iterator<String> it = param.keySet().iterator();
        String value = "";
        while(it.hasNext()){
            String key = it.next();
            Object valueObj = param.get(key);
            if(null == valueObj){
                value = "";
            }else{
                value = valueObj.toString().trim();
            }
            returnMap.put(key, value);
        }
        return returnMap;
    }
}

